<template>
  <a-layout id="components-layout-demo-custom-trigger">
    <!-- 侧边栏开始 -->
    <a-layout-sider
      v-model="collapsed"
      :trigger="null"
      collapsible
      :style="{ height: '100vh' }"
    >
      <div class="logo" />
      <a-menu
        theme="dark"
        mode="inline"
        :default-selected-keys="[this.$route.path]"
        @click="
          (nav) => {
            this.$router.push(nav.key);
          }
        "
      >
        <a-menu-item v-for="(nav, index) in navs" :key="nav.path">
          <a-icon :type="navs[index].meta.icon" />
          <span>{{ nav.name }}</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
    <!-- 侧边栏结束 -->

      <!-- 这是头部开始 -->
      <a-layout-header style="background: #fff; padding: 0">
        <a-icon
          class="trigger"
          :type="collapsed ? 'menu-unfold' : 'menu-fold'"
          @click="() => (collapsed = !collapsed)"
        />

        <!-- 用户信息 开始 -->
        <div
          :style="{ float: 'right', marginRight: '25px', cursor: 'pointer' }"
        >
          <a-dropdown placement="bottomLeft" arrow>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;">我的资料</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;">消息中心</a>
              </a-menu-item>
              <a-menu-item @click="showConfirm">
                <a href="javascript:;">退出登录</a>
              </a-menu-item>
            </a-menu>
            <div>
              <a-avatar
                src="https://img1.baidu.com/it/u=2045933031,585142074&fm=26&fmt=auto"
              />
              <span :style="{ marginLeft: '11px' }">等天晴</span>
            </div>
          </a-dropdown>
        </div>
        <!-- 用户信息 结束 -->
      </a-layout-header>
      <!-- 这是头部结束 -->

      <!-- 这是内容区开始 -->
      <a-layout-content
        :style="{
          margin: '24px 16px',
          padding: '24px',
          background: '#fff',
          minHeight: '280px',
        }"
      >
        <!-- 这里是二级路由的出口 -->
        <router-view></router-view>
      </a-layout-content>
      <!-- 这是内容区结束 -->
    </a-layout>
  </a-layout>
</template>
<script>
import { removeCookie } from 'utils/cookie'
export default {
  data () {
    return {
      collapsed: false,
      // 第一步 渲染全部的
      // navs: this.$router.options.routes[1].children

      // 第二次 过滤 加了 isNav 字段的路由
      navs: this.$router.options.routes[1].children.filter(
        (nav) => nav.meta.isNav
      )
    }
  },
  created () {
    console.log(this.navs, 'navs')
  },
  methods: {
    showConfirm () {
      this.$confirm({
        title: '确认退出登录吗?',
        onOk: () => {
          removeCookie('access_token')
          removeCookie('userInfo')
          removeCookie('role')
          this.$router.go(0)
        },
        onCancel () {}
      })
    }
  }
}
</script>
<style>
#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
</style>
